/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.helper;

import org.eclipse.uml2.uml.AcceptCallAction;
import org.eclipse.uml2.uml.AcceptEventAction;
import org.eclipse.uml2.uml.CallAction;
import org.eclipse.uml2.uml.CreateObjectAction;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.LiteralUnlimitedNatural;
import org.eclipse.uml2.uml.OpaqueAction;
import org.eclipse.uml2.uml.OutputPin;
import org.eclipse.uml2.uml.ReadExtentAction;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UnmarshallAction;
import org.eclipse.uml2.uml.ValueSpecificationAction;

/**
 * Manages an OutputPin
 *
 * @author Gilles Cannenterre
 * @since 26 mars 07
 */
public class NodeOutputPinHelper extends NodePinHelper {

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#getBaseString()
	 */
	@Override
	public String getBaseString() {
		return "OutputPin_"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public Element createUMLElement(String name, Element umlParent){
		OutputPin aPin = null;
		if (umlParent instanceof CallAction) {	
			aPin = ((CallAction)umlParent).createResult(name, null); 
		} else if (umlParent instanceof OpaqueAction) {
			aPin = ((OpaqueAction)umlParent).createOutputValue(name, null);}
		else if (umlParent instanceof AcceptEventAction) {
			aPin = ((AcceptEventAction)umlParent).createResult(name, null);
		} else if (umlParent instanceof ValueSpecificationAction) {
			aPin = ((ValueSpecificationAction)umlParent).createResult(name, null);
		} else if (umlParent instanceof UnmarshallAction) {
			aPin = ((UnmarshallAction)umlParent).createResult(name, null);
		}
		else if (umlParent instanceof ReadExtentAction) {
			aPin = ((ReadExtentAction)umlParent).createResult(name, null);
		}
		else if (umlParent instanceof CreateObjectAction) {
			aPin = ((CreateObjectAction)umlParent).createResult(name, null);
		}
		if (aPin != null) {
			LiteralUnlimitedNatural thenatural=UMLFactory.eINSTANCE.createLiteralUnlimitedNatural();
			thenatural.setValue(LiteralUnlimitedNatural.UNLIMITED);
			aPin.setUpperBound(thenatural);
		}
		return aPin;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof CallAction) {
			return ((CallAction)umlParent).getResults().add((OutputPin)element); 
		}
		if (umlParent instanceof AcceptEventAction) {
			return ((AcceptEventAction)umlParent).getResults().add((OutputPin)element);
		}
		if (umlParent instanceof ValueSpecificationAction) {
			((ValueSpecificationAction)umlParent).setResult((OutputPin) element);
			return true;
		}
		if (umlParent instanceof UnmarshallAction) {
			return ((UnmarshallAction)umlParent).getResults().add((OutputPin)element);
		}
		if (umlParent instanceof CreateObjectAction) {
			((CreateObjectAction)umlParent).setResult((OutputPin)element);
			return true;
		}
		if (umlParent instanceof ReadExtentAction) {
			((ReadExtentAction)umlParent).setResult((OutputPin)element);
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#removeUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof CallAction) {
			return ((CallAction)umlParent).getResults().remove(element); 
		}
		if (umlParent instanceof AcceptEventAction) {
			return ((AcceptEventAction)umlParent).getResults().remove(element);
		}
		if (umlParent instanceof ValueSpecificationAction) {
			((ValueSpecificationAction)umlParent).setResult(null);
			return true;
		}
		if (umlParent instanceof UnmarshallAction) {
			return ((UnmarshallAction)umlParent).getResults().remove(element);
		}
		if (umlParent instanceof CreateObjectAction) {
			((CreateObjectAction)umlParent).setResult(null);
			return true;
		}
		if (umlParent instanceof ReadExtentAction) {
			((ReadExtentAction)umlParent).setResult(null);
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if (umlParent instanceof CallAction||umlParent instanceof OpaqueAction) {
			return true;
		}
		if ( (umlParent instanceof AcceptCallAction) 
			&& ((AcceptCallAction)umlParent).getResults().isEmpty() ){
			return true;
		}
		if ( (umlParent instanceof ValueSpecificationAction) 
			&& (((ValueSpecificationAction)umlParent).getResult() == null) ){
			return true;
		}
		if ( (umlParent instanceof AcceptEventAction)
			&& !(umlParent instanceof AcceptCallAction) ){
			return true;
		}
		if (umlParent instanceof UnmarshallAction) {
			return true;
		}
		if (umlParent instanceof CreateObjectAction) {
			return true;
		}
		if (umlParent instanceof ReadExtentAction) {
			return true;
		}
		return false;
	}

}
